$(function(){
	$("#mobileForm").validate({
		rules: {
			code: 'required'
		},
		messages: {
			code: {
				required: "请输入验证码"
			}
		}
	});
	$("#mailForm").validate({
		rules: {
			code: 'required'
		},
		messages: {
			code: {
				required: "请输入验证码"
			}
		}
	});
	
	$('#validateSelect').on('change', function(){
		$('.validateForm').addClass('hidden');
		$($(this).val()).removeClass('hidden');
	});
	
	$('.js_send_mail').click(function() {
		sendMail($(this));
		disableButton.apply(this);
	});
	$('.js_send_sms').click(function() {
		sendSMS($(this));
		disableButton.apply(this);
	});
	function disableButton(){
		var interval = 180;
		var self = $(this).addClass('disabled').text(interval-- + '秒后重新发送');
		var timeout = setInterval(function() {
			if (interval == 0) {
				clearInterval(timeout);
				self.removeClass('disabled');
				self.text('免费获取验证码');
				return;
			}
			self.text(interval-- + '秒后重新发送');
		}, 1000);
	}
	function sendSMS(obj){
        $.get('/account/send/sms?mobilePhone='+obj.attr("data-value"));
	}
	function sendMail(obj){
		console.log('开始发送邮件');
        $.get('/account/email/retrieve?accountCode='+obj.attr("data-value")+"&id="+$("#js_id").val());
		console.log('邮件短信成功');
	}
});